<?php
$objXajax->registerFunction("fmfrmreportdetail_load");
$objXajax->registerFunction("fmfrmreportdetail_loadRoom");
$objXajax->registerFunction("fmfrmreportdetail_deleteDetail");
$objXajax->registerFunction("fmfrmreportdetail_setPhonenumber");
$objXajax->registerFunction("fmfrmreportdetail_saveSMS");

function fmfrmreportdetail_setPhonenumber(){
      global $lang,$objDbUpdate,$hotel_id;
    $objResponse = new xajaxResponse();// dong luon luon co khi goi ham ajax
    $sql = 'select phone_auto_send_sms as phone from cus_config where service_id = '.$hotel_id;    
    $arrPhone = $objDbUpdate->GetArray($sql);
    $phone = $arrPhone[0]['phone'];     
    $objResponse->addScript("$('#fmfrmreportdetail_txtphonenumber').val('$phone'); "); 
    return $objResponse->getXML(); // dong luon luon co khi goi ham ajax  
} 


function fmfrmreportdetail_load($dateFrom,$dateTo,$room='',$reporttype=1){     

    global $lang;
    $objResponse = new xajaxResponse();// dong luon luon co khi goi ham ajax
    $arrsum = array();
    $arrData = fmfrmreportdetail_createArray($arrsum,$dateFrom,$dateTo,$room,$reporttype);
    $count = count($arrData);
     
    $oTbs = new clsTinyButStrong();
    $oTbs->LoadTemplate('templates/'.$lang.'/customer/fm_reportdetail_grid'.$reporttype.'.html');
    $oTbs->MergeBlock('blk_data', $arrData); 
    $oTbs->MergeBlock('blk_data_sum', $arrsum);
    
    $oTbs_sms = new clsTinyButStrong();
    $oTbs_sms->LoadTemplate('templates/'.$lang.'/customer/fm_reportdetail_sms.html');
    $oTbs_sms->MergeBlock('blk_sms', $arrsum);
            
    $timerent     = $arrsum[0]['timerent'] ;
    $money_rent   = FormatMoney($arrsum[0]['money_rent']) ; 
    $money_product= FormatMoney($arrsum[0]['money_product']) ; 
    $money_arising= FormatMoney($arrsum[0]['arising_price']) ; 
    $money_voucher= FormatMoney($arrsum[0]['price_voucher']) ; 
    $total        = FormatMoney($arrsum[0]['total']) ; 
    $to_time      = $arrsum[0]['to_time'] ; 
    $to_date      = $arrsum[0]['to_date'] ; 
                    
    $smscontent = SMS_TIME_RENT.$timerent.SMS_SYSTEM_DOWNLINE.SMS_MONEY_RENT.$money_rent.SMS_SYSTEM_DOWNLINE.SMS_MONEY_PRODUCT.
    $money_product. SMS_SYSTEM_DOWNLINE.SMS_MONEY_ARISING.$money_arising.SMS_SYSTEM_DOWNLINE.
    SMS_MONEY_VOUCHER.$money_voucher.SMS_SYSTEM_DOWNLINE.SMS_MONEY_REVENUE.
    $total.SMS_SYSTEM_DOWNLINE.SMS_SYSTEM_FROMDATE.$to_time. SMS_SYSTEM_DOWNLINE.SMS_SYSTEM_TODATE.$to_date;
     
    $objResponse->addScript("fmfrmreportdetail_smscontent='$smscontent';"); 
     
    $oTbs->Show(TBS_NOTHING);  
    $objResponse->addAssign('fmfrmreportdetail_divGrid', 'innerHTML', $oTbs->Source);
    $objResponse->addScriptCall("dataTableObj.CreatDataTable('fmfrmreportdetail_tbl',true);"); 
    
    $objResponse->addAssign('fmfrmreportdetail_divsmscontent', 'innerHTML', $oTbs_sms->Source); 
     
    $objResponse->addScript('changeWaiting(1);');                     
    return $objResponse->getXML(); // dong luon luon co khi goi ham ajax   
}

function fmfrmreportdetail_createArray(&$arrSum,$dateFrom,$dateTo,$room='',$reporttype=1){
    global $objDbUpdate,$tbl_postfix;
    $addCondition = '';
    if($room!=''){
      $addCondition= " and h.room_id = '$room' ";  
    }
    $sql='';
    switch($reporttype){
        case 1:  $sql = "  select h.id,r.name as roomname,h.timestart as timebegin,h.timeend as timeend,h.arising_description,
   TIMESTAMPDIFF(MINUTE,h.timestart,h.timeend) as timerent,money_rent,total,h.price_voucher,h.arising_price,
   u1.username as userstart ,u2.username as userend  from fm_current_book$tbl_postfix as h join fm_room r 
   ON h.room_id = r.id  join tbl_user as u1 ON h.user_created = u1.id join tbl_user as u2 ON h.user_update = u2.id
     where  h.flgcal=1 and '$dateFrom'<=h.timeend and '$dateTo'>=h.timeend and h.flg_cancel = 0  $addCondition order by timeend"; 
        break;
        case 2: $sql = "  select r.name as roomname,sum(TIMESTAMPDIFF(MINUTE,h.timestart,h.timeend)) as timerent
        ,sum(money_rent) as money_rent ,sum(total) as total ,sum(h.price_voucher) as price_voucher,sum(h.arising_price) as arising_price 
        from fm_current_book$tbl_postfix as h join fm_room r ON h.room_id = r.id  
     where  h.flgcal=1 and '$dateFrom'<=h.timeend and '$dateTo'>=h.timeend and h.flg_cancel = 0 $addCondition group by h.room_id order by r.name"; 
    break;      
        case 3: $sql = "  SELECT count(staff_id) numticket,staff_id , sum(total) as total,ifnull(alias_name,name) as staff_name 
        FROM fm_current_book$tbl_postfix h left join cus_staff c on h.staff_id = c.id where  h.flgcal=1 and
         '$dateFrom'<=h.timeend and '$dateTo'>=h.timeend and h.flg_cancel = 0 $addCondition 
         group by h.staff_id order by c.alias_name"; 
    break;
    }
    $arr = array();
    try{
        $arr = $objDbUpdate->GetArray($sql);
        $count =  count($arr);
        switch($reporttype){
            case 1 :
            case 2 :
            $timerent = 0;
            $money_rent = 0;
            $total = 0;
            $price_voucher = 0;
            $arising_price = 0;
            $money_product = 0;
                for($i=0;$i<$count;$i++){
                   $arr[$i]['money_product']=(int)$arr[$i]['total'] - (int)$arr[$i]['money_rent']; 
                    $timerent +=(int)$arr[$i]['timerent'];
                    $money_rent +=(int)$arr[$i]['money_rent'];
                    $price_voucher +=(int)$arr[$i]['price_voucher'];
                    $arising_price +=(int)$arr[$i]['arising_price'];
                    $total +=(int)$arr[$i]['total'];
                    $money_product +=(int)$arr[$i]['money_product'];                    
                    $arr[$i]['timerent']= ConvertTimeToHM($arr[$i]['timerent']) ;
                    
                }
                $now =  date('Y-m-d H:i:s'); 
                if($now>$dateTo){
                  $dateTo = ConvertDate($dateTo,true);
                  $dateTo = str_replace('/','.',$dateTo);   
                }else{
                  $dateTo = date('d.m.Y H:i:s');  
                }
                $arrSum[0]['timerent']=ConvertTimeToHM($timerent);
                $arrSum[0]['money_rent']=$money_rent;
                $arrSum[0]['total']=$total;
                $arrSum[0]['money_product']=$money_product;
                $arrSum[0]['price_voucher']=$price_voucher;
                $arrSum[0]['arising_price']=$arising_price;
                $arrSum[0]['to_time']=ConvertDate($dateFrom,true);
                $arrSum[0]['to_date']=$dateTo;
            break;
        }
        
    }
    catch(Exception $e){
        $arr = array();
    } 
    return  $arr  ;
} 
function fmfrmreportdetail_loadRoom(){
    global $objDbUpdate,$strOption,$hotel_id;
    $strOption = createCombobox("fm_room",array('name','id'),array('name'=>'name','value'=>'id'),'fmfrmreportdetail_cbbroom',true,'WHERE service_id='.$hotel_id); 
    $objResponse = new xajaxResponse();    
    $objResponse->addAssign('fmfrmreportdetail_tdcbb', 'innerHTML', $strOption);
    $objResponse->addScript('fmfrmreportdetail_createReport();');
    return $objResponse->getXML(); // dong luon luon co khi goi ham ajax   
}

function fmfrmreportdetail_deleteDetail($dateDelete){
    global $objDbUpdate,$tbl_postfix,$hotel_id;
    $objResponse = new xajaxResponse();// dong luon luon co khi goi ham ajax      
    if($_SESSION['current']['power_type_id']==POWER_CUSTOMER){
        $dateDelete = addslashes($dateDelete); 
        $sql = "delete from cus_history_product$tbl_postfix where general_id in 
        (select id from fm_current_book$tbl_postfix where datediff(timeend,'$dateDelete')<0 and datediff(timeend,now())<0)"; 

        if($objDbUpdate->Execute($sql)){              
            $sql = "delete from fm_current_book$tbl_postfix where datediff(timeend,'$dateDelete')<0 and datediff(timeend,now())<0 ";
            $objDbUpdate->Execute($sql); 
            if($objDbUpdate->Execute($sql)){
              $objResponse->addScript('fmfrmreportdetail_createReport();');   
            }else{
               $objResponse->addAlert('không xóa được dữ liệu'); 
            }    
            
        }else{
         $objResponse->addAlert('không xóa được dữ liệu');    
        }
       
        
    }else{
        $objResponse->addAlert('Bạn không đủ quyền để xóa');  
    }

    $objResponse->addScript('changeWaiting(1);'); 
     return $objResponse->getXML(); // dong luon luon co khi goi ham ajax                       
}

function fmfrmreportdetail_saveSMS($phonenumber,$fmfrmreportdetail_smscontent){
     global $objDbSelect,$user_id,$hotel_id;
     $objResponse = new xajaxResponse(); 
     $arrPhone = explode(';',$phonenumber);
     $num_phone = count($arrPhone);
     $sql = "INSERT INTO tbl_sms(date_created, sms_type, sms_content, service_id, phone_number, num_phone, user_created, sms_timesend)
     values(now(),1,'$fmfrmreportdetail_smscontent',$hotel_id,'$phonenumber',$num_phone,$user_id,now())" ;
     if($objDbSelect->Execute($sql)){
       $objResponse->addScript("$('#fmfrmreportdetail_divsms').dialog('close');");     
       $objResponse->addAlert("Bạn đã gửi tin nhắn thành công");     
     }
     return $objResponse->getXML(); 
} 



# end region process data
?>
